const App = getApp();
var QQMapWX = require('../../../components/qqmap-wx-jssdk.min');
var qqmapsdk;
Page({
  data: {
    // 搜索框样式
    searchColor: "rgba(0,0,0,0.4)",
    searchSize: "15",
    searchName: "搜索商品",

    // 列表高度
    scrollHeight: 0,

    // 一级分类：指针
    curNav: true,
    curIndex: 0,

    // 分类列表
    list: [],

    // show
    notcont: false,
    department_ids:'',
    type:'',
    province:'山西省',
    city:'太原市'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    // 设置分类列表高度
    _this.setListHeight();
    // 获取分类列表
   

    _this.setData({
      department_ids:options.department_id,
      type:options.type?options.type:'0'
    })

    _this.getCategoryList();

    qqmapsdk = new QQMapWX({
      key: 'LKRBZ-BAG6U-AJSVO-45ALF-3NHL6-3XFHQ'
    });
     // 判断是否定位过,如果没有定位,就执行定位
     if(_this.data.locationStatus==false){
      _this.dingwei()
      wx.setStorageSync('locationStatus',true)
      _this.setData({
        locationStatus:wx.getStorageSync('locationStatus')
      })
    }else{ //若定位过,直接把本地的定位地址赋值
      _this.setData({
        city:wx.getStorageSync('city'),
        province:wx.getStorageSync('province')
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 更新购物车角标
  },

  /**
   * 设置分类列表高度
   */
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 0,
        });
      }
    });
  },

  /**
   * 获取分类列表
   */
  getCategoryList() {
    let _this = this;
    App._get('region/tree', {
    }, result => {
      let data = result.data;
      console.log(data)
      _this.setData({
        list: data.list,
        templet: data.templet,
        curNav: data.list.length > 0 ? data.list[0].id : true,
        notcont: !data.list.length
      });
    });
  },

  /**
   * 一级分类：选中分类
   */
  selectNav(e) {
    console.log(e)
    let _this = this;
    _this.setData({
      curNav: e.target.dataset.id,
      curIndex: parseInt(e.target.dataset.index),
      scrollTop: 0,
      province:e.currentTarget.dataset.province,
      province_id:e.currentTarget.dataset.id
    });
    wx.setStorageSync('province',_this.data.province)
    wx.setStorageSync('province_id',_this.data.province_id)
  },

  selectCity(e){
    let _this = this;
    _this.setData({
      city:e.currentTarget.dataset.city,
      city_id:e.currentTarget.dataset.city_id
    });
    wx.setStorageSync('city',_this.data.city)
    wx.setStorageSync('city_id',_this.data.city_id)

    wx.navigateBack()
  },


  /**
   * 设置分享内容
   */
  onShareAppMessage() {
    const _this = this;
    return {
      title: _this.data.templet.share_title,
      path: '/pages/category/index?' + App.getShareUrlParams()
    };
  },

  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline() {
    // const _this = this;
    // return {
    //   title: _this.data.templet.share_title,
    //   path: '/pages/category/index?' + App.getShareUrlParams()
    // };
  },


  dingwei(){
    var that = this
    console.log(1234)
   qqmapsdk.reverseGeocoder({
     success: function(res) {
       console.log(res);
       wx.setStorageSync('longitude',res.result.location.lng);
       wx.setStorageSync('latitude',res.result.location.lat);
       wx.setStorageSync('city',res.result.address_component.city)
       wx.setStorageSync('city',res.result.address_component.province)
       that.setData({
         city:wx.getStorageSync('city'),
         province:wx.getStorageSync('province')
       })
     }
    })
 },

});